package com.example.pre.controller;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.example.pre.business.UserBusiness;
import com.example.pre.controller.base.ControllerBase;
import com.example.pre.enums.UserTypeEnum;
import com.example.pre.exception.ExceptionMessage;
import com.example.pre.exception.ExtParaException;
import com.example.pre.exception.ServiceException;
import com.example.pre.model.vo.UserVO;
import com.example.pre.tools.StringUtils;
import com.example.pre.tools.Tools;

/*
 * 用户controller层接口
 */
@RestController
public class UserController extends ControllerBase {

	private final Logger logger = LoggerFactory.getLogger(UserController.class);

	@Autowired
	private UserBusiness userBusiness;

	/**
	 * 创建用户
	 * 
	 * @param name         用户名
	 * @param account      账号
	 * @param sex          性别
	 * @param password     密码
	 * @param departmentId 部门id
	 * @param type         用户类型
	 * @param pic          用户头像
	 * @return flag,message,code,data-userVO实体 ，page：分页信息
	 */
	@RequestMapping(value = "/user", method = RequestMethod.POST)
	public Map<String, Object> createUser(@RequestParam String name, @RequestParam String account,
			@RequestParam String sex, @RequestParam String password, @RequestParam Integer departmentId,
			@RequestParam UserTypeEnum type, @RequestParam String pic) {
		// 获取响应时间
		long start = System.currentTimeMillis();
		String eventId = Tools.createUUID();
		String message = "";
		String inputPara = ",传入的参数name=" + name + ", account=" + account + ", sex=" + sex + ", password=" + password
				+ ", departmentId=" + departmentId + ", type=" + type + ", pic=" + pic;
		// 预创建返回值
		HashMap<String, Object> result = new HashMap<String, Object>();
		result.put("flag", false);
		result.put("message", "OK");
		result.put("code", 0);
		result.put("data", null);

		try {
			// 参数验证
			if (StringUtils.isEmpty(name)) {
				result.put("message", "参数有误");
				result.put("code", -501);
				message = "用户名不能为空";
				logger.error(super.createLog(new Exception(), message + inputPara, start, eventId));
				return result;
			}
			if (StringUtils.isEmpty(account)) {
				result.put("message", "参数有误");
				result.put("code", -501);
				message = "用户账号不能为空";
				logger.error(super.createLog(new Exception(), message + inputPara, start, eventId));
				return result;
			}
			if (StringUtils.isEmpty(sex)) {
				result.put("message", "参数有误");
				result.put("code", -501);
				message = "用户性别不能为空";
				logger.error(super.createLog(new Exception(), message + inputPara, start, eventId));
				return result;
			}
			if (StringUtils.isEmpty(password)) {
				result.put("message", "参数有误");
				result.put("code", -501);
				message = "用户密码不能为空";
				logger.error(super.createLog(new Exception(), message + inputPara, start, eventId));
				return result;
			}
			if (departmentId == null || departmentId < 0) {
				result.put("message", "参数有误");
				result.put("code", -501);
				message = "部门id不能为空或小于0";
				logger.error(super.createLog(new Exception(), message + inputPara, start, eventId));
				return result;
			}
			if (StringUtils.isEmpty(pic)) {
				result.put("message", "参数有误");
				result.put("code", -501);
				message = "用户头像不能为空";
				logger.error(super.createLog(new Exception(), message + inputPara, start, eventId));
				return result;
			}

			UserVO userVO = userBusiness.createUser(name, account, sex, password, departmentId, type, pic);
			result.put("data", userVO);
			// 存返回数据
			message = "创建用户成功";
			result.put("flag", true);
			result.put("message", message);
			logger.info(super.createLog(message + inputPara, start, eventId));
			return result;
		} catch (ServiceException e) {
			ExceptionMessage exceptionMessage = e.getExceptionMessage();
			int code = exceptionMessage.getCode();
			result.put("code", code);
			result.put("message", exceptionMessage.getShowMessage());
			message = "创建用户时，业务报错：" + exceptionMessage.getLogMessage();
			logger.error(super.createLog(e, message + inputPara, start, eventId));
			return result;
		} catch (ExtParaException e) {
			ExceptionMessage exceptionMessage = e.getExceptionMessage();
			int code = exceptionMessage.getCode();
			result.put("code", code);
			result.put("message", exceptionMessage.getShowMessage());
			message = "创建用户时，第三方业务报错：" + exceptionMessage.getLogMessage();
			logger.error(super.createLog(e, message + inputPara, start, eventId));
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code", -400);
			result.put("message", "系统繁忙，请稍后重试");
			message = "创建用户时，系统报错：" + e.getMessage();
			logger.error(super.createLog(e, message + inputPara, start, eventId));
			return result;
		}
	}

	/**
	 * 获取用户信息
	 * 
	 * @param userId 用户id
	 * @return flag,message,code,data-userVO实体 ，page：分页信息
	 */
	@RequestMapping(value = "/user", method = RequestMethod.GET)
	public Map<String, Object> getUser(@RequestParam Integer userId) {
		// 获取响应时间
		long start = System.currentTimeMillis();
		String eventId = Tools.createUUID();
		String message = "";
		String inputPara = ",传入的参数userId=" + userId;
		// 预创建返回值
		HashMap<String, Object> result = new HashMap<String, Object>();
		result.put("flag", false);
		result.put("message", "OK");
		result.put("code", 0);
		result.put("data", null);

		try {
			if (userId == null || userId < 0) {
				result.put("message", "参数有误");
				result.put("code", -501);
				message = "用户id不能为空或小于0";
				logger.error(super.createLog(new Exception(), message + inputPara, start, eventId));
				return result;
			}

			UserVO userVO = userBusiness.getUser(userId);
			result.put("data", userVO);
			// 存返回数据
			message = "获取用户成功";
			result.put("flag", true);
			result.put("message", message);
			logger.info(super.createLog(message + inputPara, start, eventId));
			return result;
		} catch (ServiceException e) {
			ExceptionMessage exceptionMessage = e.getExceptionMessage();
			int code = exceptionMessage.getCode();
			result.put("code", code);
			result.put("message", exceptionMessage.getShowMessage());
			message = "获取用户时，业务报错：" + exceptionMessage.getLogMessage();
			logger.error(super.createLog(e, message + inputPara, start, eventId));
			return result;
		} catch (ExtParaException e) {
			ExceptionMessage exceptionMessage = e.getExceptionMessage();
			int code = exceptionMessage.getCode();
			result.put("code", code);
			result.put("message", exceptionMessage.getShowMessage());
			message = "获取用户时，第三方业务报错：" + exceptionMessage.getLogMessage();
			logger.error(super.createLog(e, message + inputPara, start, eventId));
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code", -400);
			result.put("message", "系统繁忙，请稍后重试");
			message = "获取用户时，系统报错：" + e.getMessage();
			logger.error(super.createLog(e, message + inputPara, start, eventId));
			return result;
		}
	}

}
